package woorank;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import util.TimeOut;

import exceptionPersonnaliser.UrlException;

public class SiteWebTechnologiesWoorank {
	// exemple url "exemple.com"
	private String url;
	private String cssPathInformation;
	private String cssPathDescription;
	private Document doc;
	private TimeOut timeOutSecond;
	//liste des diffirentes infromation du site
	private ArrayList<TechnologieWR> technologieSite;

	public SiteWebTechnologiesWoorank() {
		super();
		this.cssPathInformation = "h3.simple-tooltip";
		this.cssPathDescription = "div.criterium-content";
		technologieSite = new ArrayList<TechnologieWR>();
		this.timeOutSecond = TimeOut.getInstance();
	}
	
	public SiteWebTechnologiesWoorank(String url) {
		super();
		this.url = url;
		this.cssPathInformation = "h3.simple-tooltip";
		this.cssPathDescription = "div.criterium-content";
		technologieSite = new ArrayList<TechnologieWR>();
		this.timeOutSecond = TimeOut.getInstance();
	}

	public SiteWebTechnologiesWoorank(String url, String cssPathInformation,
			String cssPathDescription, Document doc, TimeOut timeOutSecond,
			ArrayList<TechnologieWR> technologieSite) {
		super();
		this.url = url;
		this.cssPathInformation = cssPathInformation;
		this.cssPathDescription = cssPathDescription;
		this.doc = doc;
		this.timeOutSecond = timeOutSecond;
		this.technologieSite = technologieSite;
	}

	public String getCssPathInformation() {
		return cssPathInformation;
	}

	public void setCssPathInformation(String cssPathInformation) {
		this.cssPathInformation = cssPathInformation;
	}

	public String getcssPathDescription() {
		return cssPathDescription;
	}

	public void setcssPathDescription(String cssPathDescription) {
		this.cssPathDescription = cssPathDescription;
	}

	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
	public Document getDoc() {
		return doc;
	}
	
	public void setDoc(Document doc) {
		this.doc = doc;
	}
	
	public TimeOut getTimeOutSecond() {
		return timeOutSecond;
	}
	
	public void setTimeOutSecond(TimeOut timeOutSecond) {
		this.timeOutSecond = timeOutSecond;
	}
	
	public ArrayList<TechnologieWR> getTechnologieSite() {
		return technologieSite;
	}
	
	public void setTechnologieSite(ArrayList<TechnologieWR> technologieSite) {
		this.technologieSite = technologieSite;
	}
	//fonction Analyser qui renvoie la liste des informations
	public ArrayList<TechnologieWR> analyser() throws UrlException {
		if (url == null) {
			throw new UrlException();
		}
		this.technologieSite.clear();
		try {
			// le site a parser
			// exemple url "exemple.com"
			doc = Jsoup.connect("http://www.woorank.com/fr/www/" + url).userAgent("Mozilla")
					.timeout(timeOutSecond.getTimeout() * 1000).get();
			// Les selecteurs CSS
			//configurer au niveau Setters ou constructeur
			Elements contentsInformation = doc.select(cssPathInformation);
			Elements contentsDescription = doc.select(cssPathDescription);
			// Objet TechnologieWR contient Information + Description
			for (int i =0 ; i < contentsInformation.size();i++) {
				this.technologieSite.add(new TechnologieWR(contentsInformation.get(i).text(),contentsDescription.get(i).text()));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return this.technologieSite;
	}
}
